package com.example.jh352160.surfingtv.Index.customView.adapter;

/**
 * Created by jh352160 on 2016/8/15
 */

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.jh352160.surfingtv.Index.childpage.ThematicList;
import com.example.jh352160.surfingtv.R;

import java.util.List;


/**
 * Created by jh352160 on 2016/7/20
 */

public class AllCategoryAdapter extends RecyclerView.Adapter<AllCategoryAdapter.ViewHolder> {

    private List<String> titles, imageUrls;
    private List<Integer> productIds;
    private Context context;

    public AllCategoryAdapter(Context context, List<String> titles, List<String> imageUrls, List<Integer> productIds) {
        // TODO: 2016/7/20 try to delete super() function
        super();
        this.titles = titles;
//        this.images=new int[]{R.drawable.commodity_goodman1,R.drawable.commodity_goodman2,R.drawable.commodity_goodman3};
        this.context = context;
        this.productIds = productIds;

        //this.imageUrls=new int[]{R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher};
//        this.names=new ArrayList<String>();
//        this.names.add("好先生1");
//        this.names.add("好先生2");
//        this.names.add("好先生3");
//        this.names.add("好先生3");
//        this.names.add("好先生3");
//
//        this.linetags=new ArrayList<String>();
//        this.linetags.add("宣传语1");
//        this.linetags.add("宣传语2");
//        this.linetags.add("宣传语3");
//        this.linetags.add("宣传语3");
//        this.linetags.add("宣传语3");
//
//        this.prices=new ArrayList<String>();
//        this.prices.add("￥100");
//        this.prices.add("￥200");
//        this.prices.add("￥300");
//        this.prices.add("￥300");
//        this.prices.add("￥300");

        this.imageUrls = imageUrls;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_index_commodity, null);
        final ViewHolder viewHolder = new ViewHolder(view);

        viewHolder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, ThematicList.class);
                intent.putExtra("contentId", viewHolder.productId);
                context.startActivity(intent);
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        //holder.image.setImageResource(imageUrls.get(position));
        Glide.with(context).load(imageUrls.get(position)).into(holder.image);
        holder.name.setText(titles.get(position));
        holder.productId = productIds.get(position);
    }

    @Override
    public int getItemCount() {
        return imageUrls.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView image;
        TextView name, linetag, price;
        int productId;

        ViewHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.commodityImage);
            name = (TextView) itemView.findViewById(R.id.commodityName);
            linetag = (TextView) itemView.findViewById(R.id.commodityTagline);
            price = (TextView) itemView.findViewById(R.id.commodityPrice);
            linetag.setVisibility(View.INVISIBLE);
            price.setVisibility(View.GONE);
        }
    }
}
